﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SYS.Web.Runtime;
using SYS.Common.Runtime;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Text;

namespace ExpertMIS
{
    public partial class Login : System.Web.UI.Page
    {
        public const string SESSION_KEY_VERIFYCODE = "_VERIFYCODE_";

        protected HttpEnvironment Environment
        {
            get
            {
                return (HttpEnvironment)EnvironmentFactory.CreateEnvironment(EnvironmentType.ASPDotNet);
            }
        }

        private bool CheckLoginInfo(string userName, string passWD)
        {
            ADMUserBroker broker = new ADMUserBroker();
            return broker.VeritifyUser(userName, passWD);
        }

        private void GeneratVerifyCode()
        {
            ExpertMIS.Codes.VerifyCode coder = new ExpertMIS.Codes.VerifyCode();

            int codeLen = 0;
            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue("verifycode.len"), out codeLen))
            {
                codeLen = 4; // 默认值
            }

            coder.CodeSerial = "0,1,2,3,4,5,6,7,8,9";
            Session[SESSION_KEY_VERIFYCODE] = coder.CreateVerifyCode(codeLen).ToLower();
            txVerifyCode.Text = "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GeneratVerifyCode();
            }
        }

        private void DoLoginFailed(string message)
        {
            StringBuilder sbScript = new StringBuilder();
            sbScript.Append("alert('" + message + "')");
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "failed", sbScript.ToString(), true);
        }

        private void DoLoginSuccessful(string account)
        {
            ADMUserBroker broker = new ADMUserBroker();
            ADMUserEntity user = broker.GetUserByAccount(account);

            ExpertMIS.Codes.Utility.GetEnvironment().UserLogon(user);

            if (string.IsNullOrEmpty(Request["requesturl"]))
            {
                if (user.IsAdmin == true)
                {
                    if (user.IsPassed == true)
                    {
                        Response.Redirect("Admin/Default.aspx");
                    }
                    else
                    {
                        Response.Redirect("Home.aspx");
                    }
                }
                else
                {
                    if (user.IsPassed == true)
                    {
                        Response.Redirect("Workships/Default.aspx");
                    }
                    else
                    {
                        Response.Redirect("Home.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect(HttpUtility.UrlDecode(Request["requesturl"]));
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txVerifyCode.Text.ToLower().Equals(Session[SESSION_KEY_VERIFYCODE]))
            {
                if (CheckLoginInfo(txUserID.Text, txPassWD.Text))
                {
                    DoLoginSuccessful(txUserID.Text);
                }
                else
                {
                    DoLoginFailed("用户名或密码无效，请重新输入！");
                    GeneratVerifyCode();
                }
            }
            else
            {
                DoLoginFailed("真输入正确的验证码！");
            }
        }
    }
}
